package com.eunion.web.cms.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eunion.core.ExcelUtil;
import com.eunion.core.LogUtil;
import com.eunion.core.exception.ValidateException;
import com.eunion.fileserver.upload.FileInfo;
import com.eunion.web.cms.entity.ScoreControllerLine;
import com.eunion.web.cms.entity.spec.ScoreControllerLineSpec;
import com.eunion.web.cms.service.ScoreControllerLineService;
import com.eunion.web.common.base.BaseService;
import com.eunion.web.common.base.BaseTableCommonController;

@Controller
@RequestMapping("/api/scoreControllerLine")
@PreAuthorize("hasAuthority('scoreControllerLine:info')")
public class ScoreControllerLineResource  extends BaseTableCommonController<ScoreControllerLine, Long> {

	@Autowired
	private ScoreControllerLineService service;
	
	@Override
	protected BaseService<ScoreControllerLine, Long> getBaseService() {
		// TODO Auto-generated method stub
		return service;
	}

	/**
     * 进入招生计划查询页面
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return getUrlBase();
    }

    @Override
    protected String getUrlBase() {
        return "admin/scoreControllerLine/scoreControllerLine";
    }
    
    /**
     * 招生计划数据导入
     * @param obj
     * @param result
     * @param model
     * @param request
     * @return
     */
    @PreAuthorize("hasAuthority('scoreControllerLine:import')")
    @PostMapping("/importData")
    public String importData(FileInfo obj, BindingResult result, Model model, HttpServletRequest request) {
        try {
            checkBindResult(result);
            if (StringUtils.isNotBlank(obj.getFilepath())) {
                String filepath = obj.getFilepath();
                String fileType = filepath.substring(filepath.lastIndexOf(".") + 1);
                InputStream in = null;

                Workbook wb = null;
                if (fileType.equals("xls")) {
                    in = new FileInputStream(filepath);
                    wb = new HSSFWorkbook(in);
                } else if (fileType.equals("xlsx")) {
                    in = new FileInputStream(filepath);
                    wb = new XSSFWorkbook(in);
                } else {
                    setModuleStatusError(model, "文件格式错误");
                    setModuleImport(model);
                    return getUrlBase().concat("-import");
                }
                
                // 开始解析数据
                Sheet sheet = wb.getSheetAt(0);
                List<ScoreControllerLine> list =  ExcelUtil.readScoreControllerLine(sheet);

                boolean ret = importUpdate(list);
                if(ret) {
                	setModuleStatusOK(model, "导入成功");
                } else {
                	LogUtil.get(this.getClass()).error("save recruitStudentsPlanList failed");
                    setModuleStatusOK(model, "导入失败");
                }
            }
        } catch (ValidateException ve) {
            setModuleStatusError(model, ve.getMsg());
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("save obj error : {}", e);
            setModuleStatusError(model, "导入失败");
        }
        setModuleImport(model);
        return getUrlBase().concat("-import");
    }

    protected void update(ScoreControllerLine fromDB,ScoreControllerLine fromFile) {
    	fromFile.setId(fromDB.getId());
    }
    
    protected Specification<ScoreControllerLine> getSpecification(ScoreControllerLine t){
    	return ScoreControllerLineSpec.build(t,true);
    }
}
